package main.Q1201_1300;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Q1241_1250 {
    public static void main(String[] args) {
        System.out.println("Question1241：");
        System.out.println("Question1242：");
        System.out.println("Question1243：");
        System.out.println("Question1244：");
        System.out.println("Question1245：");
        System.out.println("Question1246：");
        System.out.println("Question1247：");
        System.out.println("Question1248：统计「优美子数组」");
        System.out.println("Question1249：移除无效的括号");
        System.out.println("Question1250：");
    }
}

class Question1248 {
    public int numberOfSubarrays(int[] nums, int k) {
        int left = 0, right = 0, oddCount = 0, result = 0;
        while (right < nums.length) {
            if ((nums[right++] & 1) == 1) {
                oddCount++;
            }
            if (oddCount == k) {
                int tmp = right;
                while (right < nums.length && (nums[right] & 1) == 0) {
                    right++;
                }
                int rightEvenCnt = right - tmp;
                int leftEvenCnt = 0;
                while ((nums[left] & 1) == 0) {
                    leftEvenCnt++;
                    left++;
                }
                result += (leftEvenCnt + 1) * (rightEvenCnt + 1);
                left++;
                oddCount--;
            }

        }
        return result;
    }
}

class Question1249 {
    public String minRemoveToMakeValid(String s) {
        StringBuffer result = new StringBuffer();
        Stack<Integer> stack = new Stack<>();//记录待删除括号下标
        char[] str = s.toCharArray();
        for (int i = 0; i < str.length; i++) {
            if (str[i] == '(') stack.add(i);//为左括号
            else if (str[i] == ')' && !stack.isEmpty() && str[stack.peek()] == '(') stack.pop();//匹配成功
            else if (str[i] == ')') stack.add(i);//不能匹配
        }
        List<Integer> list = new ArrayList<>(stack);
        for (int i = 0; i < str.length; i++) {
            if (list.contains(i)) continue;
            result.append(str[i]);
        }
        return result.toString();
    }
}